<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/js/xtable.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/MarketRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/NoticeRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<center>
    <table style="width: 50%;" cellpadding="5" cellspacing="5">
        <tr>
            <td>
                <select id="marketId" name="marketId" onchange="getNoticePage()">
                </select>
            </td>
            <td>
                <button type="button" onclick="submitNotice('')">新增公告</button>
            </td>
        </tr>
    </table>
</center>
<SCRIPT LANGUAGE="JavaScript" >
    var noticeThead = new Array(
        new XThead("标题"),
        new XThead("内容"),
        new XThead("开始时间"),
        new XThead("截止时间"),
        new XThead("置顶"),
        new XThead("状态"),
        new XThead("修改")
    );

    var noticeXTable = new XTable("notice", noticeThead);

    var state_hash = {
        "0":"无效",
        "1":"正常"
    }

//    是否置顶
    var top_hash = {
        'false': "否",
        'true': "是"
    }

    noticeXTable.XTbodyArray = function (model) {
        var array = new Array(
            new XTbody("region", model.notice.noticeTitle, "string", null, null, "center"),
            new XTbody("region", model.notice.noticeContent.substr(0, 10), "string", null, null, "center"),
            new XTbody("region", model.notice.noticeStartTime.Format("yyyy-MM-dd"), "string", null, null, "center"),
            new XTbody("region", model.notice.noticeEndTime.Format("yyyy-MM-dd"), "string", null, null, "center"),
            new XTbody("region", top_hash[model.notice.noticeTop], "string", null, null, "center"),
            new XTbody("region", state_hash[model.notice.noticeState], "string", null, null, "center"),
            new XTbody("region", model.notice.noticeState == 1?"<a href='#' style='border-right: 1px solid #dddddd' onclick=submitNotice('"+ model.noticeId +"')>编辑&nbsp;</a>" + "<a href='#' onclick=repealNotice('"+ model.noticeId +"')>&nbsp;撤销</a>"
            :"<a href='#' style='border-right: 1px solid #dddddd' onclick=submitNotice('"+ model.noticeId +"')>编辑&nbsp;</a>" + "<a href='#' onclick=recoverNotice('"+ model.noticeId +"')>&nbsp;恢复</a>", "string", null, null, "center")
        );
        return array;
    }

    noticeXTable.getXList = function (page){
        var noticeMarket = new NoticeMarket();
        noticeMarket.pageIndex = page;
        noticeMarket.pageSize = noticeXTable.pageSize;
	    noticeMarket.marketId = dwr.util.getValue("marketId");
        NoticeRpcService.selectNoticeMarketList(noticeMarket, function(list){noticeXTable.addTable(list);});
    }
    function getNoticePage() {
        var noticeMarket = new NoticeMarket();
        noticeMarket.marketId = dwr.util.getValue("marketId");
        NoticeRpcService.selectNoticeMarketNum(noticeMarket, function(num){noticeXTable.setTotalPage(num);}) ;
    }
</SCRIPT>
</body>
</html>
<script>
    var market = new Market();
    market.marketId = "${marketId}";
    MarketRpcService.selectMarketList(market, callbackSelectMarketList);

    function callbackSelectMarketList(list) {
        dwr.util.addOptions("marketId", list, "marketId", "marketName");
        dwr.util.setValue("marketId", "${marketId}");
        getNoticePage();
    }
    function submitNotice(noticeId){
        location = "${ctx}/cms/notice_manage.jsp?noticeId=" + noticeId;
    }
    
    function repealNotice(noticeId){
    	if (noticeId == null || noticeId == '') {
            return false;
        }
    	
    	if(confirm('确定要撤销吗？')){
    		var notice = new Notice();
    		notice.noticeId = noticeId;
    		notice.noticeState = 0;
    		NoticeRpcService.updateNotice(notice, callbackUpdateNotice);
    	}else{
    		return false;
    	}
    }
    
    function recoverNotice(noticeId){
    	if (noticeId == null || noticeId == '') {
            return false;
        }
    	
    	if(confirm('确定要恢复吗？')){
    		var notice = new Notice();
    		notice.noticeId = noticeId;
    		notice.noticeState = 1;
    		NoticeRpcService.updateNotice(notice, callbackUpdateNotice);
    	}else{
    		return false;
    	}
    }
    
    function callbackUpdateNotice(data){
    	if(data){
    		alert('操作成功!');
    		location.reload();
    	}
    }

</script>
